package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeysUtil;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;

import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/checkPhone")
    public Object checkPhone(String phone){
        Boolean flag = userInfoService.checkPhone(phone);
        return flag;
    }

    @RequestMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userRegist")
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    public Object userLogin(HttpServletResponse resp,String username, String password){
        String token = userInfoService.userLogin(username,password);
        CookieUtil.addCookie("token",token,resp);
        return AjaxResult.SUCCESS;
    }

    //测试用
    @RequestMapping("/check")
    public Object userLogin(@GetUserInfoAnnotation UserInfo userInfo){
        System.out.println(userInfo);
        return AjaxResult.SUCCESS.addData(userInfo);
    }
}
